package org.feng.filter;

import cn.dev33.satoken.stp.StpUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.feng.utils.StringUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author 冯帅
 * @des 检测 是否需要登录权限
 * @date 2022/1/22 13:57
 * @email 15612509687@163.com
 */
@Component
@Data
@ConfigurationProperties("security")
@Slf4j
public class AuthFilter implements GlobalFilter, Ordered {

    private List<String> exclude;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        if (null == exclude || StringUtil.matchers(exchange.getRequest().getURI().getPath(), exclude)) {
            long end = System.currentTimeMillis();
            return chain.filter(exchange);
        } else {
            StpUtil.checkLogin();
            long end = System.currentTimeMillis();
            return chain.filter(exchange);
        }
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
